package com.tongji.housediy.view
{
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.brandEvent.GetGoodsByBrandEvent;
	import com.tongji.housediy.events.goodsEvent.FilterGoodsEvent;
	import com.tongji.housediy.model.BrandModel;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.ListTypeModel;
	
	import mx.binding.utils.BindingUtils;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class LeftDropDownMediator extends Mediator
	{
		[Inject]
		public var view:LeftDropDownList;
		
		[Inject]
		public var model:ListTypeModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		[Inject]
		public var enableModel:CompEnableStateModel;
		override public function onRegister():void
		{
			switch (view.id)
			{
				case "furnitureList2":
					BindingUtils.bindProperty(view, "dataProvider", model, "furnitureLevel2XMLList");
					this.addContextListener(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE, setVisibleHandler, TypeXMLAdminEvent);
					this.addContextListener(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_TRUE, setVisibleHandler, TypeXMLAdminEvent);
					view.selectedIndex = 0;
					break;
				case "wallTypeList2":
					BindingUtils.bindProperty(view, "dataProvider", model, "wallLevel2XMLList");
					view.selectedIndex = 0;
					break;
				case "floorList2":
					BindingUtils.bindProperty(view, "dataProvider", model, "floorLevel2XMLList");
					this.addContextListener(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE, setVisibleHandler, TypeXMLAdminEvent);
					this.addContextListener(TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_TRUE, setVisibleHandler, TypeXMLAdminEvent);	
					view.selectedIndex = 0;
					break;
				case "brandList":
					BindingUtils.bindProperty(view, "dataProvider", brandModel, "brandXMLList");
					view.selectedIndex = 0;
					break;
				case "leftBrandList":
					BindingUtils.bindProperty(view, "dataProvider", brandModel, "brandXMLList");
					BindingUtils.bindProperty(view, "selectedIndex", brandModel, "currentIndex");
					break;
			}
			
			view.addEventListener(IndexChangeEvent.CHANGE, viewChangedHandler);
			view.addEventListener(FlexEvent.VALUE_COMMIT, valueCommitHandler);
		}
		
		private function viewChangedHandler(event:IndexChangeEvent):void
		{
			var selectedIndexArray:Array = [];
			if (view.selectedIndex == 0)
			{
				for each (var tempXML:XML in view.dataProvider)
				{
					selectedIndexArray.push(tempXML.@index.toString());
				}
			}
			else
			{
				selectedIndexArray.push(view.selectedItem.@index.toString());
			}
			
			switch (view.id)
			{
				case "furnitureList2":
					dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "furnitureList1", selectedIndexArray));
					break;
				case "wallTypeList2":
					dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "wallTypeList1", selectedIndexArray));
					break;
				case "floorList2":
					dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, "floorList1", selectedIndexArray));
					break;
				case "brandList":
					dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_BRAND, "brandList", selectedIndexArray));
					break;
				case "leftBrandList":
					enableModel.brandStateIndex = 0;
					dispatch(new GetGoodsByBrandEvent(GetGoodsByBrandEvent.GET_GOODS_BY_BRANDID, view.selectedItem.@index.toString()));
					break;
			}
		}
		
		private function valueCommitHandler(event:FlexEvent):void
		{
			switch (view.id)
			{
				case "furnitureList2":
					view.selectedIndex = 0;
					break;
				case "wallTypeList2":
					view.selectedIndex = 0;
					break;
				case "floorList2":
					view.selectedIndex = 0;
					break;
				case "brandList":
					view.selectedIndex = 0;
					break;
				case "leftBrandList":
					view.selectedIndex = brandModel.currentIndex;
					break;
			}
		}
		
		private function setVisibleHandler(event:TypeXMLAdminEvent):void
		{
			if (event.type == TypeXMLAdminEvent.SET_FURNITURE_LEVEL2_VISIBLE_FALSE)
				view.visible = false;
			else
				view.visible = true;
		}
	}
}